package cn.wolfcode.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    private IDepartmentService service;
    @Autowired
    public void setService(IDepartmentService service) {
        this.service = service;
    }
    @RequestMapping("/list")
    @RequiredPermission(name="部门查询",expression = "department/list/")
    public String list(Model model, QueryObject qo){
        PageInfo<Department> PageInfo = service.query(qo);
        model.addAttribute("PageInfo",PageInfo);
        return "department/list";
    }
    @RequestMapping("/input")
    @ResponseBody
    public String input(Long id,Model model){

            Department department = service.selectById(id);

        return department.toString();
    }
    @RequiredPermission(name="部门删除",expression = "department/delete/")
  @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null){
            service.delete(id);
        }
        //这里要重定向重新回到list()里查询所有数据回显,不能在视图里寻找
        return "redirect:/department/list";
    }
    @RequiredPermission(name="部门修改/添加",expression = "department/saveOrUpdate/")
@RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department){
        if (department.getId() != null){
            service.update(department);
        }else {service.save(department);
        }

        return "redirect:/department/list";
    }


}
